﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="~/plugins/mzui/js/mzui.min.js"></script>
    <link href="~/plugins/mzui/css/mzui.min.css" rel="stylesheet" />
    <script src="~/js/jquery.js"></script>
    <title>购买卡密</title>
    <style>
        .divcenter {
            left: 38%;
            top: 18%;
            background: #fbfefd;
            color: #1210b2;
            font-size: 18px;
            overflow: hidden;
            z-index: 9999;
            position: fixed;
            padding: 5px;
            text-align: center;
            width: 380px;
            height: 238px;
            border-bottom-left-radius: 4px;
            border-bottom-right-radius: 4px;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
        }

        #divShelter {
            position: absolute;
            width: 100%;
            height: 100%;
            background: black;
            opacity: 0.6;
            filter: alpha(opacity=60);
            left: 0;
            top: 0;
        }
        .sbss {
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="divShelter" class="hidden"></div>

    <form id="buytc" class="box divcenter hidden">
        <div class="control item">
            <p class="help-text">@LJSheng.Common.LCookie.GetU("account"),你选择的卡密类型:</p>
        </div>
        <hr />
        <div class="control">
            <div id="grade" class="label dark outline">请选择套餐</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;价位:<span id="rmb" class="label important outline">0</span><input type="text" id="gid" class="hidden" />
        </div>
        <hr />
        <div class="control">
            <strong style="color:red;">购买数量</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i class="icon icon-plus" onclick="add(1);"></i> <input type="number" class="form-control" id="gmsl" value="1" placeholder="最低购买数量1" style="width:58px;text-align:center;" onkeyup="value=value.replace(/[^\d]/g,'');"> <i class="icon icon-minus" onclick="add(-1);"></i> &nbsp;&nbsp;<span id="msg" class="label label-primary label-outline blue">最少购买数量 1</span>
        </div>
        <hr />
        <div class="control">
            <span class="label label-success black">最新余额:@ViewBag.rmb</span>
        </div>
        <hr />
        <div class="control">
            <button id="wx" type="button" class="btn primary" onclick="pay();">确认</button> &nbsp;
            <button type="button" class="btn success" onclick="qx();">取消</button>
        </div>
    </form>

    <div id="tbody" class="container tiles" style="padding-top: 20px; text-align: center; font-size: 18px;">
        <script id="tpl" type="text/html">
            <div class="row">
                {{each list as item index}}
                <div id="{{item.gid}}" class="cell-3 sbss" onclick="set('{{item.name}}','{{item.gid}}','{{item.rmb}}')">
                    <div class="tile white">
                        <br />
                        <div class="label info" style="font-size:22px;height:38px;padding-top:8px;">{{item.name}}</div> <div class="label label-sm">{{item.xfx}}天</div>
                        <br /><br />
                        <div class="label dark circle">{{item.rmb}}元</div>
                        <br /><br />
                        <span class="label btn-lg label-sm success" style="font-size:22px;height:38px;width:158px; padding-top:8px;">购&nbsp;&nbsp;&nbsp;买</span>
                        <br /><br />
                    </div>
                </div>
                {{/each}}
            </div>
        </script>
    </div>

    <div id="kami" class="hidden">
        <div class="section">
            <div class="heading">
                <div class="title text-center"><h4><button class="btn">@LJSheng.Common.LCookie.GetU("account"), <span class="label label-badge black">恭喜你购买成功卡密如下</span></button></h4></div>
            </div>
            <div class="box">
                <div class="outline">
                    <textarea id="km" style="width:1388px;height:800px;"></textarea>
                </div>
            </div>
        </div>
    </div>

    <label id="pageindex" class="hidden">0</label><label id="pages" class="hidden">1</label>
    <label id="Controllers" class="hidden">/home/zctc</label>
    <script src="~/js/template-web.js"></script>
    <script src="~/js/mlist.js"></script>
    <script>
        $("#gmsl").on('input', function () {
            if (jctc()) {
                add(0);
            }
        });

        function jctc() {
            if ($("#rmb").html() > 0) {
                return true;
            }
            else {
                $("#gmsl").val("1");
                $("#msg").html("请选择套餐");
                $("#grade").html("请选择套餐").addClass("black").removeClass("red");
            }
            return false;
        }

        function add(n) {
            if (jctc()) {
                if (Number($("#gmsl").val()) > 100) {
                    $("#gmsl").val("1");
                    $("#msg").html("单笔订单限制100个卡密");
                }
                else {
                    $("#grade").removeClass("black").addClass("red");
                    $("#gmsl").val(Number($("#gmsl").val()) + n);
                    if (Number($("#gmsl").val()) <= 0) {
                        $("#gmsl").val("1");
                        $("#msg").html("最少购买数量 1");
                    }
                    else {
                        var jg = $("#rmb").html() * $("#gmsl").val();
                        var rmb = Math.round(jg * 100) / 100;
                        $("#msg").html("需要支付" + rmb);
                        if (@ViewBag.rmb < jg) {
                        $("#gmsl").val("1");
                            $("#msg").html("余额不足,无法购买更多!");
                    }
                }
            }
            }
        }

        function set(grade,gid,rmb)
        {
            $("#grade").html(grade);
            $("#wx").removeClass("hidden");
            $("#tbody div").removeClass("green");
            $("#" + gid).addClass("green");
            $("#gmsl").val("1");
            $("#rmb").html(rmb);
            $("#gid").val(gid);
            $("#divShelter").removeClass("hidden");
            $("#buytc").removeClass("hidden");
        }
        function qx() {
            $("#divShelter").addClass("hidden");
            $("#buytc").addClass("hidden");
        }
        function pay() {
            var r = confirm("购买套餐(" + $("#grade").html() + "=总价 " + $("#rmb").html() * $("#gmsl").val()+")");
            if (r == true) {
                qx();
                gopay();
            }
        }

        function gopay() {
            if ($("#grade").html() != "请选择套餐" && $("#gid").val() != "" && $("#gmsl").val() != "") {
                document.getElementById("wx").disabled = true;
                document.getElementById("wx").onclick = "javascript:void(0);";
                document.getElementById("wx").value = "提交订单中...";
                $.ajax({
                    url: "/ajax/api.ashx",
                    data: "ff=gopay&shopgid=" + $("#gid").val() + "&gmsl=" + $("#gmsl").val() + "&Cache=" + Math.random(),
                    type: "post",
                    cache: false,
                    timeout: 8000,
                    dataType: "json",
                    ContentType: "application/json; charset=utf-8",
                    async: false,//一定要是同步请求，否则会跳转；（ajax默认是异步的）
                    error: function () { location.reload(); },
                    beforeSend: function () { document.getElementById("wx").innerHTML = "提交订单中..."; document.getElementById("wx").href = "javascript:void(0);"; },
                    success: function (data) {
                        //console.log(data.return_msg);
                        if (data.appId == "payok") {
                            $("#tbody").addClass("hidden");
                            $("#kami").removeClass("hidden");
                            var reg = new RegExp("#", "g");//g,表示全部替换。
                            $("#km").val(data.return_msg.replace(reg, "\r\n"));
                        }
                        else {
                            alert(data.return_msg);
                            if (data.return_msg == "请先登录") {
                                self.location = "/home/denglu";
                            }
                        }
                    },
                });
                document.getElementById("wx").disabled = false;
                document.getElementById("wx").value = "确认";
                document.getElementById("wx").onclick = "javascript:pay();";
            }
            else
            {
                $("#grade").addClass("black");
            }
        }
    </script>
</body>
</html>